/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//		File Name		:		CMainMenuState.h
//
//		Author			:		Vindicated Games
//
//		Purpose			:		A header for all functions and members of the Main Menu
//
//		Dates			:		Written( Feb 12, 2011 ) by Vindicated Games		LastUpdate( Feb 12, 2011  ) by Vindicated Games 
//	
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include "CMainMenuState.h"

#include "../SGD Wrappers/CSGD_TextureManager.h"
#include "../SGD Wrappers/CSGD_DirectInput.h"
#include "../SGD Wrappers/CSGD_Direct3D.h"
#include "../Visual/VIN_BitMapFont.h"
#include "CSaveOrLoadState.h"
#include "CHighScoreState.h"
#include "CGamePlayState.h"
#include "../Core/CGame.h"
#include "COptionsState.h"
#include "CCredits.h"
#include "../Managers/InputManager.h"
#include "CTutorialState.h"


CMainMenuState::CMainMenuState()
{
	background = ImageID = -1;
	m_bFirstEnter = true;
	m_nscroll = -250;
	m_nSelection = 0;
}

CMainMenuState::~CMainMenuState()
{

}

void CMainMenuState::Enter()
{
	COptionsMenu::GetInstance()->LoadDefaults();
	ImageID = CSGD_TextureManager::GetInstance()->LoadTexture("resources/graphics/scroll.png");
	VIN_SoundManager::GetInstance()->PlayBGM(BGM_TITLESCREEN);
	CursorID = CSGD_TextureManager::GetInstance()->LoadTexture("resources/graphics/Sword.png");
	background = CSGD_TextureManager::GetInstance()->LoadTexture("resources/graphics/background.png");
	castle = CSGD_TextureManager::GetInstance()->LoadTexture("resources/graphics/castle.png");
}


void CMainMenuState::Exit()
{

}

CMainMenuState* CMainMenuState::GetInstance()
{
	static CMainMenuState instance;
	return &instance;
}

void CMainMenuState::Render()
{
	CSGD_Direct3D* pD3D = CSGD_Direct3D::GetInstance();
	CSGD_TextureManager* pTM = CSGD_TextureManager::GetInstance();
	VIN_BitMapFont* pBF = VIN_BitMapFont::GetInstance();
	pTM->Draw(background, 0, 0);
	pTM->Draw(castle, 530,145, 1.5f, 1.5f);
	
	pTM->Draw(ImageID, 0, (int)m_nscroll, 2.0f, 2.0f);

	if(!main_scrolling && !opt_scrolling && !game_scroll && !credit_scroll && !main_scrolling2)
	{
		pTM->Draw(CursorID, 285,175 + (75 * m_nSelection), 1.5f, 1.5f);
	}
}

bool CMainMenuState::Input()
{
	CSGD_DirectInput*	pDI = CSGD_DirectInput::GetInstance();
	CGame*				pGameState = CGame::GetInstance();

	if(!credit_scroll && !opt_scrolling && !credit_scroll && !main_scrolling && !main_scrolling2)
	{
		if(InputManager::GetInstance()->Enter())
		{
			VIN_SoundManager::GetInstance()->PlayMenuConfirm();
			VIN_SoundManager::GetInstance()->PlayMenuConfirm();
			VIN_SoundManager::GetInstance()->PlayMenuConfirm();
			VIN_SoundManager::GetInstance()->PlayMenuConfirm();
			VIN_SoundManager::GetInstance()->PlayMenuConfirm();
			VIN_SoundManager::GetInstance()->PlayMenuConfirm();
			switch(m_nSelection)
			{
			case 0:
				credit_scroll = true;
				break;
			case 1:
				opt_scrolling = true;
				break;
			case 2:
				credit_scroll = true;
				break;
			case 3:
				credit_scroll = true;
				break;
			case 4:
				PostQuitMessage(0);
				break;
			}
		}

		if(InputManager::GetInstance()->Escape())
		{
			main_scrolling = true;
		}

		if(InputManager::GetInstance()->MoveUp())
		{
			VIN_SoundManager::GetInstance()->PlayMenuSelect();
			m_nSelection--;
			if(m_nSelection < 0)
				m_nSelection = 4;
		}

		if(InputManager::GetInstance()->MoveDown())
		{
			VIN_SoundManager::GetInstance()->PlayMenuSelect();
			m_nSelection++;
			if(m_nSelection > 4)
				m_nSelection = 0;
		}
	}
	return true;
}

void CMainMenuState::Update(float fElapsedTime)
{
	if(opt_scrolling)
	{
		m_nscroll = m_nscroll - (500 * fElapsedTime);

		if(m_nscroll <= -1050.0f)
		{
			m_nscroll = -1050.0f;
			opt_scrolling = false;
			CGame::GetInstance()->ChangeState(COptionsMenu::GetInstance());
		}
	}

	else if(main_scrolling)
	{
		m_nscroll = m_nscroll + (500 * fElapsedTime);
		if(m_nscroll >= -250.0f)
		{
			m_nscroll = -250.0f;
			main_scrolling = false;
		}
	}

	else if(main_scrolling2)
	{
		m_nscroll = m_nscroll - (600 * fElapsedTime);
		if(m_nscroll <= -250.0f)
		{
			m_nscroll = -250.0f;
			main_scrolling2 = false;
		}
	}

	else if(game_scroll)
	{
		m_nscroll = m_nscroll - (500 * fElapsedTime);
		if(m_nscroll <= -2700)
		{
			m_nscroll = -2700;
			game_scroll = false;
			CGame::GetInstance()->ChangeState(CGamePlayState::GetInstance());
		}
	}

	else if(credit_scroll)
	{
		m_nscroll = m_nscroll - (600 * fElapsedTime);
		if(m_nscroll <= -1800.0f)
		{
			m_nscroll = -1800.0f;
			credit_scroll = false;
			if(m_nSelection == 3)
				CGame::GetInstance()->ChangeState( CCreditsState::GetInstance());
			else if(m_nSelection == 2)
				CGame::GetInstance()->ChangeState( CHighScoreState::GetInstance());
			else if(m_nSelection == 0)
				CGame::GetInstance()->ChangeState( CTutorialState::GetInstance());
				

				
				
		}
	}
}	